package cn.manjuu.touhou.activity.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.activity.adapter.CardInfoAdapter;
import cn.manjuu.touhou.interfaces.ShowCardInfoListener;
import cn.manjuu.touhou.util.Looger;

public class CardInfoFragment extends Fragment implements ShowCardInfoListener,
		OnClickListener {

	private FrameLayout mMainView;
	private ListView lv_card_container;
	private CardInfoAdapter mAdapter;
	private ImageView iv_show_card_info;
	private boolean isVisible;
	private RelativeLayout rl_card_cover;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mMainView = (FrameLayout) inflater.inflate(R.layout.card_info, null);
		findView();
		registerListener();
		return mMainView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		init();
	}

	private void init() {
		mAdapter = new CardInfoAdapter(this, getActivity());
		lv_card_container.setAdapter(mAdapter);
		viewGone();
		lv_card_container.setVisibility(View.VISIBLE);
	}

	private void viewGone() {
		lv_card_container.setVisibility(View.GONE);
		rl_card_cover.setVisibility(View.GONE);
		iv_show_card_info.setVisibility(View.INVISIBLE);
		iv_show_card_info.clearAnimation();
	}

	private void findView() {
		rl_card_cover = (RelativeLayout) mMainView
				.findViewById(R.id.rl_card_cover);
		lv_card_container = (ListView) mMainView
				.findViewById(R.id.lv_card_container);
		iv_show_card_info = (ImageView) mMainView
				.findViewById(R.id.iv_show_card_info);
	}

	private void registerListener() {
		// TODO Auto-generated method stub
		// lv_card_container.setOnItemClickListener(this);
		rl_card_cover.setOnClickListener(this);
	}

	@Override
	@Deprecated
	public void showCardInfoListener(View v) {
		// TODO Auto-generated method stub
		WindowManager wm = (WindowManager) getActivity().getSystemService(
				Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);

		TranslateAnimation translate = new TranslateAnimation(0f,
				outMetrics.widthPixels / 2 - v.getLeft()
						- (v.getWidth() * 3 / 2), 0f, -v.getTop());

		ScaleAnimation scale = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f);

		AnimationSet set = new AnimationSet(true);
		set.addAnimation(scale);
		set.addAnimation(translate);
		set.setDuration(1000);
		set.setFillAfter(true);

		v.setAnimation(set);
		v.startAnimation(set);
	}

	@Override
	@Deprecated
	public void showCardInfoListener(int cardId) {
		// TODO Auto-generated method stub
		viewGone();

		iv_show_card_info.setImageResource(R.drawable.pair_card_00 + cardId);
		iv_show_card_info.setVisibility(View.VISIBLE);
	}

	@Override
	public void showCardInfoListener(int cardId, View v) {
		// TODO Auto-generated method stub
		if (isVisible) {
			isVisible = !isVisible;
			viewGone();
			lv_card_container.setVisibility(View.VISIBLE);
		} else {
			rl_card_cover.setVisibility(View.VISIBLE);
			isVisible = !isVisible;
			int[] location = new int[2];
			v.getLocationInWindow(location);

			iv_show_card_info
					.setImageResource(R.drawable.pair_card_00 + cardId);

			WindowManager wm = (WindowManager) getActivity().getSystemService(
					Context.WINDOW_SERVICE);
			DisplayMetrics outMetrics = new DisplayMetrics();
			wm.getDefaultDisplay().getMetrics(outMetrics);

			// TranslateAnimation translate = new TranslateAnimation(
			// outMetrics.widthPixels / 2, outMetrics.widthPixels / 2
			// - iv_show_card_info.getLeft()
			// - (iv_show_card_info.getWidth() * 3 / 2),
			// outMetrics.heightPixels / 2, -v.getTop());
			// TranslateAnimation translate = new TranslateAnimation(
			// outMetrics.widthPixels / 2 - (iv_show_card_info.getWidth() / 2),
			// outMetrics.widthPixels / 2 - iv_show_card_info.getLeft()
			// - (iv_show_card_info.getWidth() * 3 / 2),
			// outMetrics.heightPixels / 2 - iv_show_card_info.getHeight(), -v
			// .getTop());
			int toYDelta = -(v.getHeight() / 2) + (outMetrics.heightPixels / 2)
					- (iv_show_card_info.getHeight() * 5 / 2);
			TranslateAnimation translate = new TranslateAnimation(location[0],
					outMetrics.widthPixels / 2 - iv_show_card_info.getLeft()
							- (iv_show_card_info.getWidth() * 5 / 2),
					location[1] - (v.getHeight() / 2), toYDelta);

			ScaleAnimation scale = new ScaleAnimation(1.0f, 5.0f, 1.0f, 5.0f);

			AnimationSet set = new AnimationSet(true);
			set.addAnimation(scale);
			set.addAnimation(translate);
			set.setDuration(500);
			set.setFillAfter(true);

			iv_show_card_info.setAnimation(set);
			iv_show_card_info.startAnimation(set);

			iv_show_card_info.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Looger.i(APP.CardInfoFragment, "CardInfoFragment onClick()");
		switch (v.getId()) {
		case R.id.rl_card_cover:
			if (isVisible) {
				isVisible = !isVisible;
				viewGone();
				lv_card_container.setVisibility(View.VISIBLE);
			}
			break;
		}
	}

}
